//
//  BudgetCell.m
//  预购清单
//
//  Created by  on 2022/10/16.
//

#import "BudgetCell.h"
@interface BudgetCell()
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIView *cycle;
@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (strong, nonatomic) IBOutlet UILabel *num1;
@property (strong, nonatomic) IBOutlet UILabel *num2;
@property (strong, nonatomic) IBOutlet UILabel *num3;
@property (strong, nonatomic) IBOutlet UILabel *persent;
@property (nonatomic, strong) CAShapeLayer *bgLayer;

@end
@implementation BudgetCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.bgView.layer.masksToBounds = NO;
    self.bgView.layer.shadowColor = [@"#9B9B9B" color:0.16].CGColor;
    self.bgView.layer.shadowOffset = CGSizeMake(0, 1);
    self.bgView.layer.shadowRadius = 3;
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.shadowOpacity = 1;
    self.bgLayer.strokeEnd = 1;
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.block) {
        self.block(LS(@"编辑预算"));
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (CAShapeLayer *)progressLayer{
    if (!_progressLayer) {
        _progressLayer = [self getlayer:[@"#0041C4" color]];
        [self.cycle.layer addSublayer:_progressLayer];
    }
    return _progressLayer;
}
-(CAShapeLayer *)bgLayer{
    if (!_bgLayer) {
        _bgLayer = [self getlayer:[@"#0041C4" color:0.2]];
        _bgLayer.strokeEnd = 1;
        [self.cycle.layer addSublayer:_bgLayer];
    }
    return _bgLayer;
}
- (CAShapeLayer *)getlayer:(UIColor *)color{
    CAShapeLayer *layer = [CAShapeLayer layer];
    CGFloat r = self.cycle.bounds.size.width / 2;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(r, r) radius:r-8  startAngle:M_PI*3/2 endAngle:M_PI*7/2 clockwise:YES];
    layer.frame = self.cycle.bounds;
    layer.lineWidth = 16;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = color.CGColor;
    layer.path = path.CGPath;
    layer.strokeEnd = 0;
    layer.lineCap = kCALineJoinRound;
    return layer;
}
- (void)setModel:(Budget *)model{
    _model = model;
    self.num1.text = [NSString stringWithFormat:@"%@%0.2f",LS(@"¥"),self.model.total - self.model.payed - self.model.waitPayed];
    self.num2.text = [NSString stringWithFormat:@"%@%0.2f",LS(@"¥"),self.model.payed];
    self.num3.text = [NSString stringWithFormat:@"%@%0.2f",LS(@"¥"),self.model.total];
    CGFloat p = self.model.total == 0 ? 0 : (self.model.payed + self.model.waitPayed)*1.0/self.model.total;
    self.progressLayer.strokeEnd = p;
    self.persent.text = [NSString stringWithFormat:@"%0.2f%%",(p*100)];
}
@end
